package com.quantstack.llcommonlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.quantstack.llcommonlibrary.R;
import com.quantstack.llcommonlibrary.event.OnClickEvent;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/23 14:24
 * @Notes
 */
public class CustomItem extends LinearLayout {
    private Context mContext;
    private TextView tvTitle;
    private TextView tvMessage;
    private ImageView ivIcon;

    public CustomItem(Context context) {
        this(context,null);
    }

    public CustomItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_custom_item,this);
        tvTitle = findViewById(R.id.tv_title);
        tvMessage = findViewById(R.id.tv_message);
        ivIcon = findViewById(R.id.iv_icon);
        LinearLayout llLeft = (LinearLayout)findViewById(R.id.ll_left_icon);
        llLeft.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onClickIconListener!=null){
                    onClickIconListener.onClickIcon();
                }
            }
        });

        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.CustomItem);
        int titleSize = a.getDimensionPixelSize(R.styleable.CustomItem_titleSize,14);
        int titleColor = a.getColor(R.styleable.CustomItem_titleColor,getResources().getColor(R.color.c666));
        int messageSize = a.getDimensionPixelSize(R.styleable.CustomItem_messageSize,14);
        int messageColor = a.getColor(R.styleable.CustomItem_messageColor,getResources().getColor(R.color.c333));
        String title = a.getString(R.styleable.CustomItem_title);
        String message = a.getString(R.styleable.CustomItem_message);
        Drawable icon = a.getDrawable(R.styleable.CustomItem_icon);
        if(icon!=null){
            ivIcon.setVisibility(VISIBLE);
            ivIcon.setImageDrawable(icon);
        }else{
            ivIcon.setVisibility(GONE);
        }

        a.recycle();
        tvTitle.setTextColor(titleColor);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
        tvTitle.setText(title);

        tvMessage.setTextColor(messageColor);
        tvMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP,messageSize);
        tvMessage.setText(message);
    }

    public void setMessage(String msg){
        tvMessage.setText(msg);
    }
    public void  setOnClickIconListener(OnClickIconListener onClickIconListener ){
        this.onClickIconListener  = onClickIconListener;
    }
    private OnClickIconListener onClickIconListener;
    public interface OnClickIconListener{
        void onClickIcon();
    }
}
